//仓库二级联动 局部混淆

import API from "./api";
let mixin = {
  data() {
    return {
      dataStorage: "",
      //dataLoctions: "",
      disabledOption: "",

      currentValue: {
        cangkuA1: { linkage: "", child: ["cangkuA11"] },
        cangkuA11: { linkage: "", child: "" },
        cangkuB1: { linkage: "", child: ["cangkuB11"] },
        cangkuB11: { linkage: "", child: "" }
      },

      currentList: {
        cangkuA1: "",
        cangkuA11: "",
        cangkuB1: "",
        cangkuB11: ""
      }
    };
  },

  mounted() {
    this.userInfo = this.$store.state.user.userInfo;
    this.initSelect();
  },

  methods: {
    initSelect() {
      let obj = {
        storageNums: ["1", "2", "3"],
        hasLocation: 0,
        departNum: "",
        userID: this.userInfo.userId,
        userName: "",
        b_UserRole: this.userInfo.access,
        b_DepartNum: ""
      };
      this.getStorage(obj);
    },

    getStorage(obj) {
      API.getStorage(obj)
        .then(res => {
          if (res.code == 200 && res.data) {
            let dataSto = res.data;
            this.dataStorage = dataSto.list;
            this.currentList.cangkuA1 = this.dataStorage;
            this.currentList.cangkuB1 = this.dataStorage;
          } else {
            this.dataStorage = [];
            this.$Message.success({ content: "暂无仓库！" });
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
          console.log("getStorage", err);
        });
    },

    changeStorage(key, name) {
      if (key) {
        if (name == "cangkuA1" && typeof this.disabledOption != "undefined") {
          this.disabledOption = key;
        }

        //清除本级以下的值和数组
        this.clearLinkage(name);

        //给下一级下拉数据赋值
        if (this.currentList[name + "1"] !== undefined) {
          this.currentList[name + "1"] = this.getLinkageList(
            this.currentList[name],
            key,
            name
          );
        }

        //赋值本级选择的值
        this.currentValue[name].linkage = key;
        //赋值组件选择的值
        this.callbackChangeSelect(name, this.currentValue[name]);
      }
    },

    clearLinkage(name) {
      let arr = this.currentValue[name].child;
      if (arr) {
        for (var i in arr) {
          this.currentValue[arr[i]].linkage = "";
          this.currentList[arr[i]] = "";
          this.callbackClearSelect(arr[i]);
        }
      }
    },

    getLinkageList(data, key, name) {
      let valueX = _.find(data, {
        storageNum: key
      }).locations;

      return valueX;
    },

    //组件数据赋值
    callbackChangeSelect(name, obj) {
      let objData;

      if (name.indexOf("angkuA") > -1) {
        if (this.formSearchAA) {
          objData = this.formSearchAA;
        }
      }

      if (name.indexOf("angkuB") > -1) {
        if (this.formAllotOutInList) {
          objData = this.formAllotOutInList;
        }
      }

      if (objData) {
        //console.log(138,name,obj.linkage,this.currentList[name]);
        if (name.indexOf("A11") > -1) {
          objData.localNum = obj.linkage;
          objData.localName = _.find(this.currentList[name], {
            localNum: obj.linkage
          }).localName;
        } else if (name.indexOf("A1") > -1) {
          objData.storageNum = obj.linkage;
          objData.storageName = _.find(this.currentList[name], {
            storageNum: obj.linkage
          }).storageName;
        } else if (name.indexOf("B11") > -1) {
          objData.toLocalNum = obj.linkage;
          objData.toLocalName = _.find(this.currentList[name], {
            localNum: obj.linkage
          }).localName;
        } else if (name.indexOf("B1") > -1) {
          objData.toStorageNum = obj.linkage;
          objData.toStorageName = _.find(this.currentList[name], {
            storageNum: obj.linkage
          }).storageName;
        }
      }
    },

    //组件数据清除
    callbackClearSelect(name) {
      if (typeof this.dataA != "undefined" && this.dataA != "") {
        this.dataA = [];
      }
      if (typeof this.dataAA != "undefined" && this.dataAA != "") {
        this.dataAA = [];
      }
      if (this.formSearchAA) {
        if (name == "cangkuA1") {
          this.formSearchAA.storageNum = "";
          this.formSearchAA.storageName = "";
          if (typeof this.formAllotOutInList != "undefined") {
            this.formAllotOutInList.toStorageNum = "";
            this.formAllotOutInList.toStorageName = "";
          }
        }
        if (name == "cangkuA11") {
          this.formSearchAA.localNum = "";
          this.formSearchAA.localName = "";
          if (typeof this.formAllotOutInList != "undefined") {
            this.formAllotOutInList.toLocalNum = "";
            this.formAllotOutInList.toLocalName = "";
          }
        }
      }

      if (this.formAllotOutInList) {
        if (name == "cangkuB1") {
          this.formAllotOutInList.toStorageNum = "";
          this.formAllotOutInList.toStorageName = "";
        }
        if (name == "cangkuB11") {
          this.formAllotOutInList.toLocalNum = "";
          this.formAllotOutInList.toLocalName = "";
        }
      }
      if (typeof this.formAllotOutInList != "undefined") {
        this.formAllotOutInList.toStorageNum = "";
        this.formAllotOutInList.toStorageName = "";
        this.formAllotOutInList.toLocalNum = "";
        this.formAllotOutInList.toLocalName = "";
      }
    }
  }
};

export default mixin;
